package cn.zy.org.service.impl;

import cn.zy.basic.exception.BusinessException;
import cn.zy.basic.service.impl.IBaseServiceImpl;
import cn.zy.basic.util.BaiduAiUtils;
import cn.zy.basic.util.Md5Utils;
import cn.zy.basic.util.StrUtils;
import cn.zy.org.domain.Employee;
import cn.zy.org.domain.Shop;
import cn.zy.org.domain.ShopAuditLog;
import cn.zy.org.dto.ShopDto;
import cn.zy.org.mapper.EmployeeMapper;
import cn.zy.org.mapper.ShopAuditLogMapper;
import cn.zy.org.mapper.ShopMapper;
import cn.zy.org.service.IShopService;
import cn.zy.user.domain.Logininfo;
import cn.zy.user.mapper.LogininfoMapper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.util.List;

/**
 * 业务实现类：
 */
@Service
public class IShopServiceImpl extends IBaseServiceImpl<Shop> implements IShopService {

    @Autowired
    private ShopMapper shopMapper;

    @Autowired
    private EmployeeMapper employeeMapper;

    @Autowired
    private ShopAuditLogMapper shopAuditLogMapper;

    @Autowired
    private JavaMailSender javaMailSender;

    @Autowired
    private LogininfoMapper logininfoMapper;

    @Override
    @Transactional
    public void settlement(Shop shop) {
        //一.校验
        //1.空值校验
        if(StringUtils.isEmpty(shop.getName()) || StringUtils.isEmpty(shop.getTel())){
            throw new BusinessException("店铺名和店铺电话信息不能为空!");
        }
        //2.两次密码不一致校验
        if(!shop.getAdmin().getPassword().equals(shop.getAdmin().getComfirmPassword())){
            throw new BusinessException("两次密码输入不一致!");
        }
        //3.当前店铺是否入驻过校验
        Shop dbShop = shopMapper.findByNameAndAddress(shop);
        if(dbShop != null){//从数据库找到了,说明入驻过
            throw new BusinessException("当前店铺已入驻,请直接登录或联系管理员找回密码!");
        }
        
        //二.百度AI只能审核[店铺名,logo是否合法]
        if(!BaiduAiUtils.TextCensor(shop.getName())){
            throw new BusinessException("店铺名称不合规!");
        }
        if(!BaiduAiUtils.ImgCensor("http://123.207.27.208"+shop.getLogo())){
            throw new BusinessException("店铺Logo不合规!");
        }

        //添加logininfo信息
        Employee admin = shop.getAdmin();
        //处理盐值和密码
        String salt = StrUtils.getComplexRandomString(32);
        String md5Pwd = Md5Utils.encrypByMd5(admin.getPassword() + salt);
        admin.setSalt(salt);
        admin.setPassword(md5Pwd);

        //添加logininfo信息
        Logininfo logininfo = new Logininfo();
        BeanUtils.copyProperties(admin,logininfo);
        logininfo.setType(0);//管理员
        logininfo.setDisable(true);//可以登陆
        logininfoMapper.save(logininfo);

        //三.添加到数据库
        //1.保存t_employee员工信息 - 生成自增id
        admin.setLogininfo_id(logininfo.getId());
        employeeMapper.save(admin);
        //2.再添加店铺信息
        shop.setAdminId(admin.getId());
        shopMapper.save(shop);
        //3.将店铺id更新到员工表中
        admin.setShop_id(shop.getId());
        employeeMapper.update(admin);
        //四.人工审核之后发送激活邮件
    }

    @Override
    @Transactional
    public void auditPass(ShopAuditLog log) throws MessagingException {
        //1.改状态:1待审核,2审核通过
        shopMapper.updateState(2,log.getShopId());
        //2.添加审核日志
        log.setState(1);//审核通过;
        log.setAuditId(350L);
        shopAuditLogMapper.save(log);
        //3.发邮件给店长通知激活
        MimeMessage mimeMessage = javaMailSender.createMimeMessage();
        //true - 能够添加附件
        MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true, "utf-8");
        helper.setFrom("1160898405@qq.com");
        helper.setSubject("店铺激活邮件");
        //true - 能够再内容中编写html标签 - 会解析
        Shop shop = shopMapper.loadById(log.getShopId());
        helper.setText("<h3>你的店铺已经审核通过，请<a href='http://127.0.0.1:8080/shop/active/"+log.getShopId()
                +"'>点击这里</a>激活邮件</h3>", true);
        //店铺管理员
        Employee admin = employeeMapper.loadById(shop.getAdminId());
        helper.setTo(admin.getEmail());
        //发送
        javaMailSender.send(mimeMessage);
    }

    @Override
    @Transactional
    public void auditReject(ShopAuditLog log) throws MessagingException {
        //1.改状态:1待审核,4审核失败
        shopMapper.updateState(4,log.getShopId());
        //2.添加审核日志
        log.setState(0);//审核驳回;
        log.setAuditId(350L);
        shopAuditLogMapper.save(log);
        //3.发邮件给店长通知重新修改入驻信息
        MimeMessage mimeMessage = javaMailSender.createMimeMessage();
        //true - 能够添加附件
        MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true, "utf-8");
        helper.setFrom("1160898405@qq.com");
        helper.setSubject("店铺激活失败通知");
        //true - 能够再内容中编写html标签 - 会解析
        Shop shop = shopMapper.loadById(log.getShopId());
        helper.setText("<h1>你的店铺入驻失败，请修改信息之后重新入驻</h1>",
                true);
        //店铺管理员
        Employee admin = employeeMapper.loadById(shop.getAdminId());
        helper.setTo(admin.getEmail());
        //发送
        javaMailSender.send(mimeMessage);
    }

    @Override
    @Transactional
    public void active(Long id) {
        Shop shop = shopMapper.loadById(id);
        shopMapper.updateState(3,id);

        Employee employee = employeeMapper.loadById(shop.getAdminId());
        employee.setState(1);
        employeeMapper.update(employee);
    }

    @Override
    public List<ShopDto> echarts() {
        return shopMapper.echarts();
    }
}
